<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form onsubmit="return false">
        <div>
            <label for="user">用户名:</label>
            <input name="user" type="text">
            <span class="user_span"></span>
        </div>
        <div>
            <label for="pwd">密&emsp;码:</label>
            <input name="pwd" type="text">
            <span class="pwd_span"></span>
        </div>
        <div>
            <button id="logBtn">注册</button>
            <input type="reset">
        </div>
    </form>
</body>
<script>
    var userInp = document.querySelector("input[name = 'user']");
    var pwdInp = document.querySelector("input[name = 'pwd']");
    var btn = document.getElementById("logBtn");

    // post
        // 1. post提交数据是 将参数数据队列作为一个参数放在send中
        // 2. 设置请求头(陪住提交的表单数据类型Content-Type: text/html;charset=utf-8)

    btn.onclick = function() {
        var user = userInp.value;
        var pwd = pwdInp.value;

        if(user && pwd) {
            var xhr = new XMLHttpRequest();  

            xhr.open("post", "../php/request.php", true); 
            // xhr.open("post", "../php/post.php", true); 
            
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");       //设置请求头
            xhr.send(`user=${user}pwd=${pwd}`);
            xhr.onreadystatechange = function() {
                if(xhr.readyState == 4 && xhr.status == 200) {
                    console.log(xhr.responseText);
                }
            }
        }
    }
    
</script>
</html>